<?php
namespace app\api_wp_v01\controller;

class RecycleOrderLong extends ApiWechatProgram {
	
	public function get_listing(){
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('target', $parameter) && is_string($parameter['target']) && in_array($parameter['target'], \app\common\model\RecycleOrderLongModel::$status_alias);
		$judge_2 = array_key_exists('page', $parameter) && is_integer($parameter['page']) && (0 < $parameter['page']);
		if(!$judge_1 || !$judge_2){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$where = array();
		if('all' !== $parameter['target']){
			$status_alias = array_flip(\app\common\model\RecycleOrderLongModel::$status_alias);
			$where['recycle_order_long.status'] = $status_alias[$parameter['target']];
		};
		
		$field = array(
			'`recycle_order_long`.`id`', '`recycle_order_long`.`action_date`', '`recycle_order_long`.`action_time`',
			'`recycle_order_long`.`address`', '`recycle_order_long`.`address_describe`',
			'`region`.`name_full` AS `region_name`'
		);
		$order_select = \app\common\model\RecycleOrderLongModel::field($field)
			->join('region', '`region`.`id` = `recycle_order_long`.`region_district`', 'LEFT')
			->where('recycle_order_long.subscriber_id', $this->subscriber_id)
			->where($where)
			->order('recycle_order_long.id DESC')
			->limit(10)->page($parameter['page'])
			->select();
		if(0 === count($order_select)){
			return $this->disposeJson(1, '没有更多的数据了');
		};
		
		$order_list = array();
		foreach($order_select as $RecycleOrderLongModel){
			$order = array(
				'id' => $RecycleOrderLongModel->id,
				'action_date' => date('Y-m-d', $RecycleOrderLongModel->action_date),
				'action_time' => $RecycleOrderLongModel->action_time,
				'region_name' => $RecycleOrderLongModel->region_name,
				'address' => $RecycleOrderLongModel->address,
			);
			$order_list[] = $order;
		};
		
		return $this->disposeJson(0, '', array(
			'order_list' => $order_list,
		));
	}
	
	public function get_detail() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$RecycleOrderLongModel = \app\common\model\RecycleOrderLongModel::get($parameter['id']);
		if(null === $RecycleOrderLongModel){
			return $this->disposeJson(1, '目标不存在');
		}else if($this->subscriber_id !== $RecycleOrderLongModel->subscriber_id){
			return $this->disposeJson(1, '该订单不属于你');
		};
		
		$data = array(
			'number' => $RecycleOrderLongModel->identifier,
			'action_date' => date('Y-m-d', $RecycleOrderLongModel->action_date),
			'action_time' => $RecycleOrderLongModel->action_time,
			'remark' => $RecycleOrderLongModel->remark,
			'liaison_name' => $RecycleOrderLongModel->liaison_name,
			'liaison_phone' => $RecycleOrderLongModel->liaison_phone,
			'region_name' => '',
			'address' => $RecycleOrderLongModel->address,
			'address_describe' => $RecycleOrderLongModel->address_describe,
			'create_time' => date('Y-m-d H:i:s', $RecycleOrderLongModel->create_time),
			'frequency_name' => $RecycleOrderLongModel->getFrequencyName(),
			'status' => $RecycleOrderLongModel->status,
			'status_name' => $RecycleOrderLongModel->getStatusName(),
			'can_cancel' => false,
			'cancel_time' => '',
			'cancel_cause' => '',
		);
		
		// 获取地区
		$RegionModel = \app\common\model\RegionModel::get($RecycleOrderLongModel->region_district);
		if(null === $RegionModel){
			return $this->disposeJson(1, '获取地区信息失败');
		};
		$data['region_name'] = $RegionModel->name_full;
		
		// 判断订单状态
		switch($RecycleOrderLongModel->status){
			case 0 :
				$data['cancel_time'] = date('Y-m-d H:i:s', $RecycleOrderLongModel->cancel_time);
				$data['cancel_cause'] = $RecycleOrderLongModel->cancel_cause;
				break;
				
			case 1 :
				$data['can_cancel'] = true;
				break;
		};
		
		// 获取员工信息
		if(1 < $RecycleOrderLongModel->status){
			$StaffModel = \app\common\model\StaffModel::get($RecycleOrderLongModel->staff_id);
			if(null === $StaffModel){
				return $this->disposeJson(1, '对应的员工未找到');
			};
			$data['staff_realname'] = $StaffModel->realname;
			$data['staff_phone'] = $StaffModel->phone;
		};
		
		return $this->disposeJson(0, '', $data);
	}
	
	public function cancel() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$RecycleOrderLongModel = \app\common\model\RecycleOrderLongModel::get($parameter['id']);
		if(null === $RecycleOrderLongModel){
			return $this->disposeJson(1, '目标未找到');
		}else if(1 !== $RecycleOrderLongModel->status){
			return $this->disposeJson(1, '目标状态不正确');
		};
		
		$time = time();
		$result = \app\common\model\RecycleOrderLongModel::where('id', $RecycleOrderLongModel->id)->where('status', 1)
			->update([
				'status' => 0,
				'cancel_time' => $time,
				'cancel_cause' => '用户主动取消',
			]);
		if(1 !== $result){
			return $this->disposeJson(1, '修改订单状态失败');
		};
		
		return $this->disposeJson(0, '操作成功');
		
	}
	
}